package com.campus.merchant.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.merchant.bean.merchant.MerchantBusinessHoursSaveBean;
import com.campus.merchant.entity.MerchantBusinessHours;
import com.campus.merchant.mapper.MerchantBusinessHoursMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商户营业时间表（支持多时段设置） 服务实现类
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-04-18
 */
@Service
public class MerchantBusinessHoursService extends ServiceImpl<MerchantBusinessHoursMapper, MerchantBusinessHours> {
    @Autowired
    private MerchantBusinessHoursMapper merchantBusinessHoursMapper;

    /**
     * 保存商户营业时间列表（先删后增）
     * @param bean 参数
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveBusinessHoursList(MerchantBusinessHoursSaveBean bean) {
        // 先删除该商户原有营业时间
        lambdaUpdate().eq(MerchantBusinessHours::getMerchantId, bean.getMerchantId()).remove();
        // 批量插入新数据
        List<MerchantBusinessHours> list = new ArrayList<>();
        for (MerchantBusinessHoursSaveBean.BusinessHourItem item : bean.getBusinessHoursList()) {
            MerchantBusinessHours mbh = new MerchantBusinessHours();
            mbh.setMerchantId(bean.getMerchantId());
            mbh.setStartTime(item.getStartTime());
            mbh.setEndTime(item.getEndTime());
            mbh.setWeekdays(item.getWeekdays());
            list.add(mbh);
        }
        if (!list.isEmpty()) {
            saveBatch(list);
        }
    }
}
